package test.solr;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import com.alibaba.fastjson.JSON;

public class Evelation {
	
	@Test
	public void testInsert() {
		HttpSolrClient client = new HttpSolrClient("http://localhost:8080/solr/core1");
		
		try {
			List<SolrInputDocument> idocs = new ArrayList<>(); 
			
			SolrInputDocument idoc = new SolrInputDocument();
			idoc.addField("id", 99999991);
			idoc.addField("name", "排名第一");
			idoc.addField("category", "特殊分类");
			idoc.addField("brand", "官方");
			idoc.addField("jd_price", 1000000);
			idoc.addField("market_price", 1000000);
			idoc.addField("company", "solr专营店");
			idoc.addField("evaluate_count", 99999);
			idoc.addField("ctime", new Date());
			
			SolrInputDocument idoc2 = new SolrInputDocument();
			idoc2.addField("id", 99999992);
			idoc2.addField("name", "排名第二");
			idoc2.addField("category", "特殊分类");
			idoc2.addField("brand", "官方");
			idoc2.addField("jd_price", 1000000);
			idoc2.addField("market_price", 1000000);
			idoc2.addField("company", "solr专营店");
			idoc2.addField("evaluate_count", 99999);
			idoc2.addField("ctime", new Date());
			
			SolrInputDocument idoc3 = new SolrInputDocument();
			idoc3.addField("id", 99999993);
			idoc3.addField("name", "排名第三");
			idoc3.addField("category", "特殊分类");
			idoc3.addField("brand", "官方");
			idoc3.addField("jd_price", 1000000);
			idoc3.addField("market_price", 1000000);
			idoc3.addField("company", "solr专营店");
			idoc3.addField("evaluate_count", 99999);
			idoc3.addField("ctime", new Date());
			
			idocs.add(idoc);
			idocs.add(idoc2);
			idocs.add(idoc3);
			
			client.add(idocs);
			client.commit();
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SolrServerException e) {
			e.printStackTrace();
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		HttpSolrClient client = new HttpSolrClient("http://localhost:8080/solr/core1");
		SolrQuery query = new SolrQuery("name:小米");
//		query.setParam("df", "name");
		
//		SolrQuery query = new SolrQuery("name:小米");
		
		query.setRequestHandler("/elevate");
		
		query.setParam("enableElevation", true);
		try {
			QueryResponse qr = client.query(query);
			System.out.println(JSON.toJSONString(qr.getResponse().asMap(Integer.MAX_VALUE), true));
			
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SolrServerException e) {
			e.printStackTrace();
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Test
	public void test2() throws IOException {
		String query = "小米手机";
		StringBuilder norm = new StringBuilder("");
		Analyzer analyzer = new IKAnalyzer();
		try (TokenStream tokens = analyzer.tokenStream("", query)) {
	      tokens.reset();

	      CharTermAttribute termAtt = tokens.addAttribute(CharTermAttribute.class);
	      while (tokens.incrementToken()) {
	    	  System.out.println(termAtt.toString());
	        norm.append(termAtt.buffer(), 0, termAtt.length());
	      }
	      tokens.end();
	    }
		System.out.println(norm);;
	}

}
